import { IpcMsg, MessageChannelType } from "./types/Ipc";
import { Event, ipcRenderer } from "electron";
import { SimulateController } from "./Controller";
import { WinName } from "./types/Win";
import * as remote from "@electron/remote";

export default class IpcService {

  controller: SimulateController;

  constructor(c: SimulateController) {
    this.controller = c;
  }

  send(windowName: WinName, type: MessageChannelType, data?: any) {
    ipcRenderer.send(IpcMsg.MessageChannel, {
      windowName,
      msg: { type, data }
    });
  }
  sendCustomize(type: IpcMsg, data: any) { // reconstruct temporary
    ipcRenderer.send(type, data);
  }

  breakpointChange() {
    ipcRenderer.on("BreakpointChange", (e: Event, data: { projectId: string}) => {
      if (this.controller.projectId === data.projectId) {
        this.controller.breakpointsService.refreshFromModel();
        this.controller.breakpointsService.notifyEdition();
      }
    });
  }

  projectClose() {
    ipcRenderer.on("CloseProject", (e: Event, data: { projectId: string }) => {
      if (this.controller.projectId === data.projectId) {
        const simWin = remote.getCurrentWindow();
        simWin.close();
      }
    });
  }
}
